package com.user.tool;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.Scanner;

import com.user.applaction.UserApp;
import com.user.command.CommandEnum;
import com.user.pojo.User;

/**
 * Created by sunchengqi on 2017/7/12.
 */
public class Tool {

	private static Scanner scanner = new Scanner(System.in);

	/**
	 * 功能：输出帮助文件
	 */
	public static void printHelp() {
		String filePath = "/Users/sunchengqi/Documents/workspace/usermanager/help.txt";
		try {
			File file = new File(filePath);
			FileReader fileReader = new FileReader(file);
			BufferedReader bufferedReader = new BufferedReader(fileReader);
			String line = bufferedReader.readLine();
			while (line != null) {
				System.out.println(" ->  " + line);
				line = bufferedReader.readLine();
			}
			bufferedReader.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 功能：打印提示消息，判断非空输入
	 * 
	 * @param message
	 * @return 返回不为空的输入
	 */
	public static String askUserInputNotNull(String message) {
		String input;
		printMsg(message);
		while (true) {
			printCmd();
			input = scanner.nextLine().trim();
			if (!"".equals(input)) {
				return input;
			}
			printMsg("Input cannot be empty :(");
		}
	}

	public static String askUserInput() {
		String input;
		int count = 0;
		while (true) {
			printCmd();
			input = scanner.nextLine().trim().toUpperCase();
			if ("".equals(input)) {
				count++;
				if (count < 2) {
					printMsg("Can't be empty!");
				} else if (count >= 2) {
					printMsg("Please check your input again!");
				}
				continue;
			} else if (!isLegalCommand(input)) {
				printMsg("Command does not exist!");
				continue;
			} else if (isLegalCommand(input)) {
				break;
			}
		}
		return input;

	}

	private static boolean isLegalCommand(String input) {
		try {
			CommandEnum.valueOf(input);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	/**
	 * 功能：打印 "msg> @param message"
	 * 
	 */
	public static void printMsg(String message) {
		System.out.println(message);
	}

	/**
	 * 功能：打印 "cmd> "
	 */
	public static void printCmd() {
		if (UserApp.getUser().getId() != 0) {
			if ("".equals(UserApp.getUser().getUserName())) {
				System.out.print("improve your information ~ cmd > ");
				return;
			} else if (null == UserApp.getUser().getUserName()) {
				System.out.print("improve your information ~ cmd > ");
				return;
			}
		}
		if (UserApp.getUser().getId() == 0) {
			System.out.print("no user ~ cmd > ");
			return;
		}
		System.out.print(UserApp.getUser().getUserName() + " ~ cmd > ");
	}

	public static String EncoderByMd5(String s) {
		return MD5Encrypt.encode(s);
	}

	public static void printFormate(List<User> user) {
		System.out.println(
				"+----+------------+--------------+----------+-----------+------------+-----------+-----------+");
		System.out.println(
				"| id | userId     | userPassword | userName | userBirth | userGender | userPhone | userEmail |");
		// System.out.println("+----+------------+--------------+----------+-----------+------------+-----------+-----------+");
		int count = 0;
		for (int i = 0; i < user.size(); i++) {
			System.out.println(
					"+----+------------+--------------+----------+-----------+------------+-----------+-----------+");
			System.out.print("| " + user.get(i).getId() + " |");
			System.out.print(" " + user.get(i).getUserId() + " |");
			System.out.print(" " + user.get(i).getUserPassword());

			System.out.print(" |\n");

		}
		System.out.println(
				"+----+------------+--------------+----------+-----------+------------+-----------+-----------+");
	}
}
